﻿<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<meta name="apple-mobile-web-app-capable" content="yes" />
		<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
		<meta name="apple-mobile-web-app-title" content="思路广惠">
		<title>购物车</title>
		<meta content="telephone=no" name="format-detection" />
		<link rel="stylesheet" type="text/css" href="../../src/css/base.css?1.0" />
		<style type="text/css">

		</style>
	</head>

	<body class="body-bg">
		<header class="header">
			<div class="header-txt">购物车</div>
			<a href="javascript: void(0)" class="head-ri" id="edit" index="0">编辑</a>
		</header>
		<section class="cart">
			<div id="addcart">
				<p style="text-align: center; margin-top: 50%;"><img style="width: 4rem; height: 4rem;" src="../../src/images/loading.gif" /></p>
			</div>
			<!--购物车为空-->
			<div style="display: none; text-align: center;" id="nullcar">
				<img src="../../src/images/null-gouwuchechatu.png" />
				<h1 class="col3 pad-lr">购物车还是空的</h1>
				<p class="s11m btm1rem">快去商城选购商品吧~</p>

				<a class="cashCancel" style="background: none;" href="../home/homeclassify.html?type=1">去逛逛 </a>

			</div>
		</section>
		<div class="cart-footer">
			<div class="cart-footer-l">

				<input type="checkbox" id="che" class="dress-dio" name="aip" />

				<label for="che" style="margin-right: 1rem;">全选</label>
				<div id="edit-pri">
					<p>现金： ￥<span id="carcash" class="huef36">0</span> </p>
					<p>积分： <span id="carpoint" class="huef36">0</span> </p>
				</div>
			</div>
			<div class="cart-footer-r">
				<button onclick="window.location.href='orderIndex.html'" id="edit-buy">结算</button>
				<button type="button" id="edit-buys" style="display:none;">删除</button>
			</div>
		</div>
		<!--底部导航 -->
		<div class="bottom-nav">
			<div class="sc-box">
				<a href="../home/homeintegral.html?type=2"><img src="../../src/images/button_jifenshangcheng.png" /></a>
				<a href="../home/homeoffline.html?type=2"><img src="../../src/images/button_xianxiashangcheng.png" /></a>
			</div>
			<footer class="centre flexs-al setup-u">
				<a class="flex1" href="../home/index.html?type=0">
					<i class="iconfont icon-five"></i>
					<p>首页</p>
				</a>
				<a class="flex1" href="../home/homeclassify.html?type=1">
					<i class="iconfont icon-one"></i>
					<p>分类</p>
				</a>
				<a class="flex1" href="javascript:void(0)" id="slicksShop" index=0>
					<div class="switch">
						<img src="../../src/images/button_close.png" />
					</div>
					<div class="switch">
						<i class="iconfont icon-four"></i>
						<p>丝路商城</p>
					</div>
				</a>
				<a class="flex1" href="../order/index.html?type=3">
					<i class="iconfont icon-three"></i>
					<p>购物车</p>
				</a>
				<a class="flex1" href="../center/index.html?type=4">
					<i class="iconfont icon-anonymity"></i>
					<p>我的</p>
				</a>
			</footer>
		</div>
		<script type="text/html" id="addcarts">
			{{# for(var i = 0; i
			< d.Return_Data.length; i++){ }} <div class="edit-items btm1rem">
				<div class="edit-name flexs">
					<input type="checkbox" class="dress-dio" name="oip" data-i="{{i}}" />
					<a href="../home/homestore.html?{{d.Return_Data[i].Business_ID}}"> {{d.Return_Data[i].Business_Name}}</a>
				</div>
				{{# for(var j = 0; j
				< d.Return_Data[i].CarProduct.length; j++){ }} <div class="edit-item flexs" data-type={{d.Return_Data[i].CarProduct[j].Car_ID}}>
					<div>
						<input type="checkbox" class="dress-dio" name="iip" data-j="{{j}}" data-i="{{i}}" data-a="{{i}}" />
					</div>
					<a class="flexs" href="../order/details.html?typeID={{d.Return_Data[i].CarProduct[j].Product_ID}}">
						<figure class="edit-img">
							<img class="wh100" src={{d.img}}{{d.Return_Data[i].CarProduct[j].Image_Url}} />
						</figure>
						<div class="edit-r">
							<p> {{d.Return_Data[i].CarProduct[j].Product_Name}} </p>
							<p>{{d.Return_Data[i].CarProduct[j].ProductSku_Name}}</p>
							<p class="edit-rp flexs">
								<span class="huef36">
                                    ￥<span class="carcash">{{d.Return_Data[i].CarProduct[j].ProductSku_PriceCash}}</span> {{# if(d.Return_Data[i].CarProduct[j].Product_BuyType == 1){ }} + 积分 <span class="carpoint">{{d.Return_Data[i].CarProduct[j].ProductSku_PricePoints}}</span> {{# } }}
								</span>
								<span class="ord-num">×<span class="carindex">{{d.Return_Data[i].CarProduct[j].Car_Amount}}</span></span>
							</p>
						</div>
					</a>
					</div>
					{{# } }}
					</div>
					{{# } }}
		</script>
		<script type="text/html" id="editcarts">

			{{# for(var i = 0; i
			< d.Return_Data.length; i++){ }} <div class="edit-items btm1rem">

				<div class="edit-name flexs">
					<input type="checkbox" class="dress-dio" name="oip" data-i="{{i}}" />
					<a href="../home/homestore.html?{{d.Return_Data[i].Business_ID}}"><span> {{d.Return_Data[i].Business_Name}} </span></a>
				</div>
				{{# for(var j = 0; j
				< d.Return_Data[i].CarProduct.length; j++){ }} <div class="edit-item flexs" data-type={{d.Return_Data[i].CarProduct[j].Car_ID}}>
					<div>
						<input type="checkbox" class="dress-dio" name="iip" data-j="{{j}}" data-i="{{i}}" data-a="{{i}}" />
					</div>
					<div class="flexs" href="../order/details.html?typeID={{d.Return_Data[i].CarProduct[j].Product_ID}}">
						<figure class="edit-img">
							<img class="wh100" src={{d.img}}{{d.Return_Data[i].CarProduct[j].Image_Url}} />
						</figure>
						<div class="edit-r">
							<div class="edit-rnum flexs" data-j="{{j}}" data-i="{{i}}">
								<button class="amount-sub blocks w25 centre s13m"> - </button>
								<input type="text" class="amount-num details-bd blocks w50 centre s13m" onkeyup="this.value = this.value.replace(/^0|[^\d]/g, '')" value={{d.Return_Data[i].CarProduct[j].Car_Amount}} />
								<button class="amount-sup blocks w25 centre s13m"> + </button>
							</div>

							<p>{{d.Return_Data[i].CarProduct[j].ProductSku_Name}}</p>
						</div>
						 
					</div>
					</div>
					{{# } }}
					</div>
					{{# } }}
		</script>
		<script src="http://apps.bdimg.com/libs/laytpl.js/1.1/laytpl.js"></script>
		<script src="//cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
		<script src="//cdn.bootcss.com/layer/3.0.1/mobile/layer.js"></script>
		<script src="../../src/js/all.js" type="text/javascript" charset="utf-8"></script>
		<script>
			var edit = null; // 全局变量
			// 获取购物车
			(function() {

				!MembersID && (location.replace("../login/index.html?backurl=" + location.pathname));

				silks.temp(["#addcarts", "#addcart"], "/workcar/getcar", {
					MembersID: MembersID
				}, function() {
					edit = this, // 后台数据，操作更改同步
 					console.log(edit)
					if(edit.Return_Data.length == 0) {
						$("#nullcar").show();
						$("#addcart,.cart-footer").hide();
					}
					// 点击删除
					$("#edit-buys").click(function() {
						var cartdet = [],
							ddi     = [];
						$("input[name='iip']").each(function() {
							if(this.checked) {
								var di = this.getAttribute("data-i"),
									dj = this.getAttribute("data-j");
								cartdet.push(edit.Return_Data[di].CarProduct[dj].Car_ID); // 存储选中的id
								ddi.push([di, dj]);
							}
						});

						if(cartdet.length) {
							var data = {
								MembersID: MembersID,
								CarID_Array: cartdet.join("|") + "|",
								AppID: AppID,
								Token: Token
							};
							silks.loaded(true);
							$.post(urls + "/WorkCar/DeleteCar", data, function(res) {
								if(res.Return_ID == 0) {
									for(var i = 0; i < cartdet.length; i++) { // 移除选中移除的订单
										for(var j = 0; j < edit.Return_Data.length; j++) {
											for(var k = 0; k < edit.Return_Data[j].CarProduct.length; k++) {
												if(cartdet[i] == edit.Return_Data[j].CarProduct[k].Car_ID) {
													edit.Return_Data[j].CarProduct.splice(k, 1);
												}
											}
											if(!edit.Return_Data[j].CarProduct.length) {
												edit.Return_Data.splice(j, 1)
											}

										}

									}

									laytpl($("#editcarts").html()).render(edit, function(html) {
										$("#addcart").html(html);
									});
									silks.loaded(false);
									if(edit.Return_Data.length == 0) {
										$("#nullcar").show();
										$("#addcart,.cart-footer").hide();
									}
								} else {
									layers(res.Return_Mess, function() {
										silks.loaded(false);
									})
								}

							});
						}
					});

				});
			}());

			// 点击编辑
			$("#edit").click(function() {
				if(edit.Return_Data.length == 0) {
					$("#nullcar").show();
					$("#addcart,.cart-footer").hide();
				}
				var $ts = $(this),
					$ix = $ts.attr("index"),
					$sh = function(a, b, c, d) {

						$(a[0]).hide();
						$(a[1]).show();
						$ts.attr("index", b);
						$ts.html(c);

						laytpl($(d).html()).render(edit, function(html) {
							$("#addcart").html(html);
						});

					};

				Number($ix) ? $sh(["#edit-buys", "#edit-pri,#edit-buy"], 0, "编辑", "#addcarts") : ($sh(["#edit-pri,#edit-buy", "#edit-buys"], 1, "完成", "#editcarts"), /*商品数量加减*/ silks.goodsNum(changeNum));

				// 重置全选以及值计算
				$("#che").get(0).checked = false;
				$("#che").get(0).className = "dress-dio";
				$("#carcash").html(0);
				$("#carpoint").html(0);

			});

			// 改变数量
			function changeNum(num) {

				var $eta = $(this).closest(".edit-rnum"),
					$i = $eta.attr("data-i"),
					$j = $eta.attr("data-j");
				 
				silks.loaded(true);
				$.post(urls + "/WorkCar/EditCar", {
					MembersID: MembersID,
					CarID: $(this).closest(".edit-item").attr("data-type"),
					Amount: num,
					AppID: AppID,
					Token: Token
				}, function(res) {
					if(res.Return_ID == 0) {
						  
						edit.Return_Data[$i].CarProduct[$j].Car_Amount = num; //改变保存的数量与数据同步
						silks.loaded(false);
						 
					} else {
						layers(res.Return_Mess, function() {
							silks.loaded(false);
						})
					}

				});

			}
		</script>
		<script>
			silks.fooChoo(); // 商城选择点击
			silks.checkMark(); // 复选框选中 价格变化
		</script>
	</body>
</html>